/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author Administrator
 */
import java.awt.*;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.*;
import java.awt.event.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintTestzzg extends JFrame {

    PrintPanel canve;
    JTable aa;
    JButton Jbtn;
    HashPrintRequestAttributeSet attribute;
    JScrollPane jscrollpane;

    public PrintTestzzg() {
        canve = new PrintPanel();
        setSize(400, 300);

        Jbtn = new JButton("点我打印");

        Jbtn.addActionListener(new JbtnAction());
        attribute = new HashPrintRequestAttributeSet();

        jscrollpane = new JScrollPane(canve);



        canve.setPreferredSize(new Dimension(3000, 3000));


        add(jscrollpane, BorderLayout.CENTER);
        add(Jbtn, BorderLayout.SOUTH);
        validate();
        setVisible(true);

    }

    public static void main(String[] args) {
        new PrintTestzzg();



    }

    class JbtnAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            PrinterJob aa = PrinterJob.getPrinterJob();
            aa.setPrintable(canve);
            if (aa.printDialog(attribute)) {
                try {
                    aa.print(attribute);
                } catch (PrinterException e1) {
                    e1.printStackTrace();
                }


            }

        }
    }

    class PrintPanel extends JPanel implements Printable {

        public PrintPanel() {
            setLayout(new BorderLayout());

//
            JPanel panel1 = new JPanel();
            panel1.setLayout(new BorderLayout());
            panel1.add(new JButton("5555"), BorderLayout.CENTER);
            add(panel1, BorderLayout.EAST);

            setBackground(Color.BLUE);



            JButton cc = new JButton("kkkkkkk");
            add(cc, BorderLayout.SOUTH);
            validate();
        }

        public void paint(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            drawPage(g2);
        }

        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
            if (page >= 1) {
                return Printable.NO_SUCH_PAGE;
            }
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));

            drawPage(g2);



            return Printable.PAGE_EXISTS;
        }

        public void drawPage(Graphics2D g2) {

            super.paint(g2);

            g2.drawLine(0, 0, 300, 600);

        }
    }
}
